package com.cari.oa.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.cari.oa.domain.CustomerContactEntity;
import com.cari.oa.util.StrUtils;

@Repository
@SuppressWarnings("unchecked")
public class CustomerContactDao extends BaseDao<CustomerContactEntity> {
	
	/**
	 * 分页查找客户联系人信息
	 * @author -- liuyoujie
	 * @2013-6-14 下午4:52:19
	 * @param name		客户联系人姓名
	 * @param company	客户所在单位
	 * @param province	客户所在省份
	 * @param page		当前页
	 * @param size		每页显示的条数
	 * @return
	 */
	public List<CustomerContactEntity> getPage(String name, String company, String province,
			Timestamp beginTime, Timestamp endTime, Integer page, Integer size){
		StringBuilder stringBuilder = new StringBuilder("from CustomerContactEntity c where 1=1 ");
		
		if(StrUtils.isNotEmpty(name))	stringBuilder.append(" and c.name like :name ");
		if(StrUtils.isNotEmpty(company)) stringBuilder.append(" and c.company like :company");
		if(StrUtils.isNotEmpty(province))	stringBuilder.append(" and c.province = :province");
		if(beginTime != null) stringBuilder.append(" and c.createTime > :beginTime ");
		if(endTime != null)	stringBuilder.append(" and c.createTime < :endTime ");
		
		stringBuilder.append(" order by c.createTime desc");
		
		Query query = getSession().createQuery(stringBuilder.toString());
		if(StrUtils.isNotEmpty(name))	query.setParameter("name", "%" + name + "%");
		if(StrUtils.isNotEmpty(company))	query.setParameter("company", "%" + company + "%");
		if(StrUtils.isNotEmpty(province))	query.setParameter("province", province);
		if(beginTime != null)	query.setParameter("beginTime", beginTime);
		if(endTime != null)		query.setParameter("endTime", endTime);
		
		query.setFirstResult((page - 1)* size).setMaxResults(size);
		return query.list();
	}
	
	/**
	 * 查询符合条件的记录总数
	 * @author -- liuyoujie
	 * @2013-6-14 下午4:54:09
	 * @param name		客户姓名
	 * @param company	客户所在单位
	 * @param province	客户所在省份
	 * @return
	 */
	public long getCount(String name, String company, String province, Timestamp beginTime, Timestamp endTime){
		StringBuilder stringBuilder = new StringBuilder("select count(*) from CustomerContactEntity c where 1=1 ");
		
		if(StrUtils.isNotEmpty(name))	stringBuilder.append(" and c.name like :name ");
		if(StrUtils.isNotEmpty(company)) stringBuilder.append(" and c.company like :company");
		if(StrUtils.isNotEmpty(province))	stringBuilder.append(" and c.province = :province");
		if(beginTime != null) stringBuilder.append(" and c.createTime > :beginTime ");
		if(endTime != null)	stringBuilder.append(" and c.createTime < :endTime ");
		
		Query query = getSession().createQuery(stringBuilder.toString());
		if(StrUtils.isNotEmpty(name))	query.setParameter("name", "%" + name + "%");
		if(StrUtils.isNotEmpty(company))	query.setParameter("company", "%" + company + "%");
		if(StrUtils.isNotEmpty(province))	query.setParameter("province", province);
		if(beginTime != null)	query.setParameter("beginTime", beginTime);
		if(endTime != null)		query.setParameter("endTime", endTime);
		
		return (Long) query.uniqueResult();
	}
	
}
